<template>
  <div class="container">
    <div class="p16" style="width:800px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="背景图片">
          <el-upload
            action=""
            list-type="picture-card"
            :http-request="handleCustomUpload"
            :before-upload="handleUploadBefore"
            :show-file-list="false"
          >
            <div class="modelBox" v-if="form.url.url" @click.stop>
              <div class="model">
                <i class="el-icon-zoom-in" @click="handlePictureCardPreview"></i>
                <i class="el-icon-delete" @click="handleDeleteUpload"></i>
              </div>
              <img  :src="form.url.url" class="el-upload--picture-card" />
            </div>
            <i slot="default" class="el-icon-plus" v-else></i>
          </el-upload>
          <div class="tips">请上传jpg、png格式，尺寸建议为1920px*300px</div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-image-viewer
      v-if="isShowImgView"
      :url-list="dialogImageUrl"
      :initial-index="0"
      :on-close="closeViewer"
      :z-index="3000"
    />
    <!-- <div class="demo-image__preview">
      <el-image-viewer
        style="width: 100px; height: 100px"
        v-if="isShowImgView"
        :src="form.url.url"
        :zoom-rate="1.2"
        :initial-index="0"
        @close="isShowImgView=false"
        fit="cover"
      />
    </div> -->
  </div>
</template>
<script>
  import {
    arraySort
  } from "@/utils";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "Materiel",
    components: {
      "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer"),
    },
    computed: {
      ...mapGetters(["name"]),
    },
    data() {
      return {
        form: {
          url:{
            url:''
          }
        },
        isShowImgView:false,
        dialogImageUrl:[],
      };
    },
    created() {
    },
    mounted() {
      this.getData()
    },
    updated() {},
    methods: {
      getData(){
        this.dialogImageUrl = []
        this.$api.system.GetList(10).then((res)=>{
        this.form = res[0]
        this.dialogImageUrl.push(this.form.url.url)
        })
      },

      handleSave(){
        this.$api.system.Update(this.form).then(res=>{
          this.$message.success('保存成功！')
          this.getData()
        })
      },

      handleUploadBefore(e){
        console.log(e)
      },

      //图片上传
      handleCustomUpload(e){
        const file = e.file;
      const type = file.name.substring(file.name.lastIndexOf(".") + 1);
      if (
        type !== "png" &&
        type !== "jpg" &&
        type !== "jpeg" &&
        type !== "gif" &&
        type !== "bmp"
      ) {
        this.$message.error("上传图片格式错误，请重新上传!");
        return false;
      } else {
          var formData = new FormData();
            formData.append("file", file);
            formData.append("output", 'json2');
            this.$api.system.upload(formData).then((res) => {
              this.dialogImageUrl = []
              let prefix = '/assets'
              this.form.url.url = prefix+res.src
              console.log(this.form)
              this.dialogImageUrl.push(this.form.url.url)
              this.$message.success("上传成功！");
            });
      }
      },

      //预览
      handlePictureCardPreview (file) {
        this.isShowImgView = true
      },

      //删除图片
      handleDeleteUpload(){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.form.url.url = ''
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
        });
      },

      //关闭预览
      closeViewer(){
        this.isShowImgView = false
      }
    },
  };

</script>
<style lang="scss" scoped>

::v-deep .el-upload ,.el-upload--picture-card{
  width: 100% !important;
}
::v-deep .el-upload,.el-upload--picture-card{
  height: 200px !important;
}
.container::v-deep{
  $size:148px;
  margin-bottom: 8px;

  .tips{
    font-size:13px;
    color:rgba(153, 153, 153, 1);
  }
  .modelBox{
    width: 100%;
    height: 200px;
    .model{
      display: none;
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.5);
      z-index: 2;
      >*{
        font-size: 20px;
        color: #fff;
        &:hover{
          color: #3f61f1;
        }
        &:not(:last-child){
          margin-right: 20px;
        }
      }
    }
    &:hover{
      .model{
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
  .el-upload{
    width: #{$size};
    height: #{$size};
    position: relative;
    display:flex;
    align-items:center;
    justify-content:center;
    .tag{
      position: absolute;
      left: 0;
      top: 0;
      padding: 4px 6px;
      line-height: 1.2em;
      background: rgba(0, 0, 0, 0.75);
      color: #fff;
      font-size: 12px;
      border-radius: 6px 0 4px 0;
      z-index: 2;
    }
  }
  .img_box{
    width: #{$size};
    height: #{$size};
    position: relative;
    img{
      max-width: 100%;
      min-width: 100%;
    }
  }
  .img_boxMore{
    display: flex;
    flex-wrap: wrap;
    margin-right: -8px;
    margin-bottom: -8px;
    .img_box{
      border-radius:8px;
      border: 1px #dcdfe6 solid;
      overflow: hidden;
      margin-right: 8px;
    }
    >*{
      margin-right: 8px;
      margin-bottom: 8px;
    }
  }
}

</style>
